package com.example.tse.rbyair.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import com.example.tse.rbyair.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

public class RbImageLoader {

	

	private static final int MAX_CACHE_SIZE = 10 * 1024 * 1024;

//	private static final int LruMemoryCache = 10 * 1024 * 1024;
	public static final ImageLoader mImageLoader = ImageLoader.getInstance();
	public  static final String FILE = "file:///";
	public static final String HTTP = "http://";
	private static final String IMAGELOADER_PATH = "rbyair/imageloader";
	private static DisplayImageOptions options;
//	private static AnimateFirstDisplayListener animateFirstListener;
	public static final String TAG = RbImageLoader.class.getSimpleName();
	private static AnimateFirstDisplayListener animateFirstListener;
	
	
	
	/** 
	 * 第一次初始化，;之后获取使用ImageLoader.getInstance();
	 */
	public static void init(Context context) {
		File cacheDir = StorageUtils.getOwnCacheDirectory(context, IMAGELOADER_PATH);
		com.nostra13.universalimageloader.utils.L.disableLogging();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		.diskCache(new UnlimitedDiskCache(cacheDir))
		.threadPoolSize(3)
		.threadPriority(Thread.NORM_PRIORITY - 2)
		 .memoryCacheExtraOptions(480, 800) 
		.denyCacheImageMultipleSizesInMemory()
//		.memoryCache(new MemoryCache(LruMemoryCache))
		.memoryCache(new WeakMemoryCache())
		.defaultDisplayImageOptions(getImageOptions())
		.diskCacheSize(MAX_CACHE_SIZE)
		.build();
		mImageLoader.init(config);
	}

	/** 
	 * 圆角
	 */
	public static DisplayImageOptions getRoundImageOptions(int degree) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_goodscontent)
		.showImageForEmptyUri(R.drawable.occupying_goodscontent)
		.showImageOnFail(R.drawable.occupying_goodscontent)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.displayer(new RoundedBitmapDisplayer(degree))
		.build();
		return options;
	}
	/** 
	 * 圆角
	 */
	public static DisplayImageOptions getSencondRoundImageOptions(int degree) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_goodscontent)
				.showImageForEmptyUri(null)
				.showImageOnFail(R.drawable.occupying_goodscontent)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.displayer(new RoundedBitmapDisplayer(degree))
				.build();
		return options;
	}
	
	
	/**
	 * 个人中心 头像
	 * @param degree
	 * @return
	 */
	public static DisplayImageOptions getPersonalRoundImageOptions(int degree) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.user_defaul)
		.showImageForEmptyUri(R.drawable.user_defaul)
		.showImageOnFail(R.drawable.user_defaul)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.displayer(new RoundedBitmapDisplayer(degree))
		.build();
		return options;
	}
	
	/** 
	 * faxian 圆角
	 */
	public static DisplayImageOptions getSortRoundImageOptions(int degree) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_goodscontent)
		.showImageForEmptyUri(R.drawable.occupying_goodscontent)
		.showImageOnFail(R.drawable.occupying_goodscontent)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.displayer(new RoundedBitmapDisplayer(degree))
		.build();
		return options;
	}
	
	public static DisplayImageOptions getImageBigShowOption(){
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(false)
				.cacheOnDisk(false)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.showImageOnFail(R.drawable.occupying_goodscontent)
				.showImageForEmptyUri(R.drawable.occupying_goodscontent)
				.build();
				return options;
	}
	
	
	public static DisplayImageOptions getImageOptions(int degree) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher).showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(false).cacheOnDisk(true).displayer(new RoundedBitmapDisplayer(degree)).build();
		return options;
	}

	/**
	 * 获取方图blanck,底图
	 * @return
	 */
	public static DisplayImageOptions getImageOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_square).showImageForEmptyUri(R.drawable.occupying_square)
				.showImageOnFail(R.drawable.occupying_square).cacheInMemory(false).cacheOnDisk(true).build();
		return options;
	}
	
	/**
	 * 获取正方图blanck,底图
	 * @return
	 */
	public static DisplayImageOptions getSqureImageOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_square).showImageForEmptyUri(R.drawable.occupying_square)
				.showImageOnFail(R.drawable.occupying_square).cacheInMemory(false).cacheOnDisk(true).build();
		return options;
	}

	/**
	 * 获取商品详情正方图blanck,底图
	 * @return
	 */
	public static DisplayImageOptions getDetailSqureImageOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_goodscontent).showImageForEmptyUri(R.drawable.occupying_goodscontent)
				.showImageOnFail(R.drawable.occupying_goodscontent).cacheInMemory(true).cacheOnDisk(true).build();
		return options;
	}
	/**
	 * 图片浏览
	 */
	public static DisplayImageOptions getImageViewPagerOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(false).cacheOnDisk(true).build();
		return options;
	}

	public static DisplayImageOptions getLogoOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_shopping)
				.showImageForEmptyUri(R.drawable.occupying_shopping).showImageOnFail(R.drawable.occupying_shopping).cacheInMemory(false).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}
	
	public static DisplayImageOptions getBannerOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_banner)
				.showImageForEmptyUri(R.drawable.occupying_banner).showImageOnFail(R.drawable.occupying_banner).cacheInMemory(false).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}
	
	public static DisplayImageOptions getProductDetialOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_goodscontent)
				.showImageForEmptyUri(null).showImageOnFail(R.drawable.occupying_goodscontent).cacheInMemory(false).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}
	
	public static DisplayImageOptions getSmallLogOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_shopping)
				.showImageForEmptyUri(null).showImageOnFail(R.drawable.occupying_shopping).cacheInMemory(true).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}
	public static DisplayImageOptions getSmallOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.occupying_shopping)
				.showImageForEmptyUri(R.drawable.occupying_goodscontent).showImageOnFail(R.drawable.occupying_shopping).cacheInMemory(true).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}

	public static DisplayImageOptions getUserImgOptions() {
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.user_defaul)
				.showImageForEmptyUri(R.drawable.user_defaul).showImageOnFail(R.drawable.user_defaul).cacheInMemory(false).cacheOnDisk(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.build();
		return options;
	}
	
	/** 
	 * 图片显示方式，透明显示
	 */
	public static ImageLoadingListener getAnimateFadeInListener() {
		if (animateFirstListener == null) {
			animateFirstListener = new AnimateFirstDisplayListener();
		}
		return animateFirstListener;
	}

	/**
	 * @description 清chu图片缓存；
	 */

	public static void removeImageCache(String url) {
		if (TextUtils.isEmpty(url)) {
			return;
		}
		MemoryCacheUtils.removeFromCache(url, ImageLoader.getInstance().getMemoryCache());
		DiskCacheUtils.removeFromCache(url, ImageLoader.getInstance().getDiskCache());
	}


	/** 
	 * 将缓存冲的图片文件删除
	 */
	public static void remove(String path) {
		DiskCacheUtils.removeFromCache(path, mImageLoader.getDiskCache());
		MemoryCacheUtils.removeFromCache(path, mImageLoader.getMemoryCache());
	}

	/** 
	 * 默认显示图片方式
	 */
	public static void displayImage(String url, ImageView imageView) {
		mImageLoader.displayImage(url, imageView, getImageBigShowOption(),getAnimateFadeInListener());
	}

	/**
	 * 默认没有图片
	 */
	public static void displayImageWithoutDefult(String url ,ImageView imageView){
		mImageLoader.displayImage(url, imageView, getAnimateFadeInListener());
	}
	
	/** 
	 * 显示圆角
	 */
	public static void displayImage(String url, int degree, ImageView imageView) {
		mImageLoader.displayImage(url, imageView, getRoundImageOptions(degree), getAnimateFadeInListener());
	}
	/** 
	 * 显示圆角
	 */
	public static void displayImageSecond(String url, int degree, ImageView imageView) {
		mImageLoader.displayImage(url, imageView, getSencondRoundImageOptions(degree), getAnimateFadeInListener());
	}
	
	//getPersonalRoundImageOptions
	public static void displayPersonalImage(String url, int degree, ImageView imageView) {
		mImageLoader.displayImage(url, imageView, getPersonalRoundImageOptions(degree), getAnimateFadeInListener());
	}
	
	//getSortRoundImageOptions
	/** 
	 * sort显示圆角
	 */
	public static void displaySortImage(String url, int degree, ImageView imageView) {
		mImageLoader.displayImage(url, imageView, getSortRoundImageOptions(degree), getAnimateFadeInListener());
	}

	/**
	 * @description 显示图片，option 不同 长方形图片，正方形图片
	 */

	public static void displayImage(String url, ImageView imageView, DisplayImageOptions options) {
		mImageLoader.displayImage(url, imageView, options, getAnimateFadeInListener());
	}

}
